<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : product_datecall.class.php   FILE_PATH : E:\www\\trunk\classes\application\product_datecall.class.php
 * ....关于商品数据调用
 *
 * @copyright workflow_system 
 * @author workflow Develop Team 
 * @package 
 * @subpackage 
 * @version Wed Jul 02 14:09:03 CST 2008
 */
defined('Base_Use_Sign') or die("this is invalid system!please contact <a href='http://www.shopnc.net'>www.shopnc.net</>.");
class ProductDatecallClass extends FrameWork {
	/**
	 * 调用列表 
	 * @author workflow Develop Team     
	 * @param  $condition,$obj_page
	 * @return array
	 */
	function listDatecall($condition,&$obj_page){
		$conditionstr = $this->_conditionSearch($condition);
		$datecall_array = $GLOBALS['db']->GetList($conditionstr, $obj_page, "product_datecall");
		return $datecall_array;
	}

	/**
	 * 构造检索条件
	 * @author workflow Develop Team     
	 * @param  
	 * @return string
	 */
	function _conditionSearch($condition){
		$conditionstr = "";
		//排序
		if ($condition['order'] != ''){
			$conditionstr .= " order by ";
			$conditionstr .= $condition['order'];
		}
		return $conditionstr;
	}

	/**
	 * 新增数据调用 
	 * @author workflow Develop Team     
	 * @param  $value_array
	 * @return bool
	 */
	function addDatecall($value_array){
		if (is_array($value_array)){
			$valueArray = array();
			foreach ($value_array as $k => $v){
				if ($v != ''){
					$valueArray[$k] = $v;
				}
			}
			$result = $GLOBALS['db']->InsertRow($valueArray,"product_datecall","pd_id");
			unset($value_array,$valueArray);
			return $result;
		}
		return false;
	}

	/**
	 * 根据ID取调用记录
	 * @author workflow Develop Team     
	 * @param  $id
	 * @return array
	 */
	function getDatecall($id){
		if (intval($id) > 0){
			$value_array = $GLOBALS['db']->GetOneRow(intval($id),'product_datecall','pd_id');
			return $value_array;
		}
		return false;
	}

	/**
	 * 取最大ID
	 * @author workflow Develop Team     
	 * @param  
	 * @return int
	 */
	function getLastId(){
		$datecall_array = $GLOBALS['db']->GetList('',$page,'product_datecall','*',0,'pd_id desc');
		$last_id = $datecall_array[0]['pd_id'];
		unset($datecall_array);
		return $last_id;
	}

	/**
	 * 修改信息
	 * @author workflow Develop Team     
	 * @param  $value_array
	 * @return bool
	 */
	function updateDatecall($value_array){
		if (is_array($value_array) && intval($value_array['pd_id']) > 0){
			$valueArray = array();
			foreach ($value_array as $k => $v){
				$valueArray[$k] = $v;
			}
			$result = $GLOBALS['db']->UpdateRow(intval($valueArray['pd_id']),$valueArray, 'product_datecall', 'pd_id');
			unset($value_array,$valueArray);
			return $result;
		}
		return false;
	}

	/**
	 * 删除信息
	 * @author workflow Develop Team     
	 * @param  $id
	 * @return bool
	 */
	function delDatecall($id){
		if (intval($id) > 0){
			$result = $GLOBALS['db']->DeleOneRow(intval($id),'product_datecall','pd_id');
//			@unlink('../html/product_javascript/'.md5($id).'.js');
			return $result;
		}
		return false;
	}
	
	/**
	 * 生成JS调用文件
	 * @author workflow Develop Team     
	 * @param  $id
	 * @return bool
	 */
	function make_js_file($id,$site_url,$charset){
		//判断缓存文件夹是否存在，不存在则创建
		if (!is_dir("../html/product_javascript")){
			//如果不能改变文件夹权限,则返回错误
			if (!mkdir("../html/product_javascript",0777)) {
				return false;
			}
		}
		//取调用记录
		$datecall_array = $this->getDatecall($id);
		if ($datecall_array['pd_id'] == ''){
			return false;
		}
		//创建商品对象
		if (!is_object($obj_product)) {
			require_once('product.class.php');
			$obj_product = new ProductClass();
		}
		//初始化变量
		$this->_getConfiginfo();
		//根据查询条件取商品
		if ($datecall_array['datecall_num'] != '') {
			$condition['limit_num'] = $datecall_array['datecall_num'];
		}
		if ($datecall_array['condition_p_cate'] != '') {
			$condition['search_cate'] = $datecall_array['condition_p_cate'];
		}
		if ($datecall_array['condition_price_left'] != '') {
			$condition['price_min'] = $datecall_array['condition_price_left'];
		}
		if ($datecall_array['condition_price_right'] != '') {
			$condition['price_max'] = $datecall_array['condition_price_right'];
		}
		if ($datecall_array['condition_keyword'] != '') {
			$condition['key'] = $datecall_array['condition_keyword'];
		}
		if ($datecall_array['condition_sort'] != '') {
			$condition['sorttype'] = $datecall_array['condition_sort'];
		}
		$product_list = $obj_product->getProductList($condition,$page);
		//整理商品信息，截取商品名称，判断是否存在商品静态页
		if ($datecall_array['p_name_size'] != '') {
			if (is_array($product_list)){
				foreach ($product_list as $k => $v){
					$product_list[$k]['cut_p_name'] = Char_class::cut_str($product_list[$k]['p_name'],$datecall_array['p_name_size'],0,$charset);
					$product_list[$k] = $obj_product->checkOneProductIfHtml($product_list[$k],$this->_configinfo['productinfo']['ifhtml']);
				}
			}
		}
		
		$datecall_array['templates_head'] = str_replace("\"","\\\"",$datecall_array['templates_head']);
		$datecall_array['templates_head'] = preg_replace("((\r\n)+)", '', $datecall_array['templates_head']);
		$datecall_array['templates_connect'] = str_replace("\"","\\\"",$datecall_array['templates_connect']);
		$datecall_array['templates_connect'] = preg_replace("((\r\n)+)", '', $datecall_array['templates_connect']);
		$datecall_array['templates_footer'] = str_replace("\"","\\\"",$datecall_array['templates_footer']);
		$datecall_array['templates_footer'] = preg_replace("((\r\n)+)", '', $datecall_array['templates_footer']);
		
		//模板变量array
		$variable_arr = array(
			'{product_pic_url}' => 'p_pic',         //商品图片链接地址
			'{product_url}' => 'product_url',       //商品链接地址
			'{product_full_name}' => 'p_name',      //商品全称
			'{product_name}' => 'product_name',     //截取后的商品名称
			'{product_price}' => 'p_price'          //商品单价
		);

		//生成JS格式的文件内容
		if (is_array($product_list)){
			$line = '';
			for ($i=0;$i<count($product_list);$i++){
				$line .=  "document.write(\"";
				$line .= $datecall_array['templates_head'];
				//取模板内容中所用到的模板变量并赋值
				foreach ($variable_arr as $key => $val){
					$str = strstr($datecall_array['templates_connect'],$key);
					if ($str != '') {
						if ($val == 'product_url') {
							$url = $product_list[$i]['html_url'] != '' ? $product_list[$i]['html_url'] : $this->_configinfo['websit']['site_url'] . 'home/product.php?action=view&pid=' . $product_list[$i]['p_code'];
							$datecall_array['templates_connect'] = str_replace($key,$url,$datecall_array['templates_connect']);
						}elseif ($val == 'product_name') {
							$datecall_array['templates_connect'] = str_replace($key,$product_list[$i]['cut_p_name'],$datecall_array['templates_connect']);
						}elseif ($val == 'p_pic') {
							$datecall_array['templates_connect'] = str_replace($key,$this->_configinfo['websit']['site_url'] . '/' . $product_list[$i]['p_pic'],$datecall_array['templates_connect']);
						}else {
							$datecall_array['templates_connect'] = str_replace($key,$product_list[$i][$val],$datecall_array['templates_connect']);
						}
					}
				}
				$line .= $datecall_array['templates_connect'];
				$line .= $datecall_array['templates_footer'];
				$line .= "\");";
			}
		}
		//文件名
		$file_name = '../html/product_javascript/'.md5($id).'.js';
		//生成缓存文件
		require_once("makehtml.class.php");
		$result = MakeHtml::installhtmlfile($file_name, $line);
		//销毁变量
		unset($is_array,$cid_array,$conditon_news,$news_list,$line,$file_name);
		return $result;
	}
}
?>